//
//  StringUtiltity.m
//  NSArraybinarySearch
//
//  Created by huchu on 2018/8/17.
//  Copyright © 2018年 sysjones. All rights reserved.
//

#import "SYStringUtiltity.h"
#import "YYKit.h"

@implementation SYStringUtiltity

// 身份证
+(BOOL)checkIdentityCard:(NSString *)string {
    NSString *regex=@"(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL r = [predicate evaluateWithObject:string];
    if (r) {
        if (string.length == 18) {
            int numberArray[17] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
            char checkbit[11] = {'1','0', 'X','9', '8', '7', '6', '5', '4', '3', '2'};
            int result  = 0;
            for (int i = 0; i < 17; i ++) {
                result += numberArray[i] * [[string substringWithRange:NSMakeRange(i, 1)]intValue];
            }
            result = result%11;
            char lastCharacter = [string characterAtIndex:17];
            //NSLog(@"末尾数字是%c，正确的校验码%c",lastCharacter,checkbit[result]);
            if (checkbit[result]!=lastCharacter) {
                r= NO;
            }
        }
    }
    return r;
}
 
+(BOOL)checkQQ:(NSString *)string{
    NSString *regex=@"^[1-9][0-9]{4,}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL r = [predicate evaluateWithObject:string];
    return r;
}
+(BOOL)checkPhone :(NSString *)string{
    NSString *regex=@"^1[0,1,2,3,4,5,6,7,8,9]\\d{9}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL r = [predicate evaluateWithObject:string];
    return r;
}
+(BOOL)checkEmail :(NSString *)string{
    NSString *regex=@"[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL r = [predicate evaluateWithObject:string];
    return r;
}

+(BOOL)checkMixtureParssword:(NSString *)string{
    NSString *regex=@"^((?=.*\\d)(?=.*[a-zA-Z])|(?=.*\\d)(?=.*[\\W_])|(?=.*[a-zA-Z])(?=.*[\\W_])).{6,18}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL r = [predicate evaluateWithObject:string];
    return r;
}

+(BOOL)checkPureDigitalParssword:(NSString *)string{
    NSString *regex=@"^[0-9]{6}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL r = [predicate evaluateWithObject:string];
    return r;
}

+(BOOL)checkUserLoginName:(NSString*)string {
    NSString *regex=@"^[a-zA-Z0-9_-]{2,15}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL r = [predicate evaluateWithObject:string];
    return r;
}

+(BOOL)checkAmount:(NSString*)string{
    NSString *regex=@"^(([1-9]\\d{0,9})|0)(\\.\\d{1,2})?$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL r = [predicate evaluateWithObject:string];
    return r;
}

+(BOOL)checkBankCard:(NSString *)string {
    NSString *regex=@"^[0-9]{15,25}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL r = [predicate evaluateWithObject:string];
    return r;
}
+(BOOL)checkPostalCode:(NSString *)string {
    NSString *regex=@"^[1-9]\\d{5}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL r = [predicate evaluateWithObject:string];
    return r;
}
@end
